package top.stmo.blog.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import top.stmo.blog.dao.mapper.ArticleMapper;
import top.stmo.blog.dao.pojo.Article;

@Component
public class ThreadService {

    //期望此操作在线程池执行，不会影响原有的主线程
    @Async("taskExecutor")
    public void updateArticleViewCount(ArticleMapper articleMapper, Article article) {

        int viewCounts = article.getViewCounts();
        Article articleUpdate = new Article();
        articleUpdate.setViewCounts(viewCounts + 1);
        LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(Article::getId,article.getId())
        //为了在多线程的环境下 线程安全 设置一个乐观锁
                .eq(Article::getViewCounts,viewCounts);
        articleMapper.update(articleUpdate,updateWrapper);
        try {
            Thread.sleep(5000);
            System.out.println("更新完成了。。。。。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
